import re
from datetime import date
import time

def parse_when(summary):
    text = summary.text
    when = text.split('<br />')[0]
    start = _datefrmstr(when.split(' to ')[0].split(': ')[1])
    end = None
    if len(when.split(' to ')) == 2:
        end = _datefrmstr(when.split(' to ')[1])
    return (start, end)

def _datefrmstr(string):
    parts = string.split(' ')
    datestring = ' '.join((parts[1], parts[2], re.search('\d+', parts[3]).group(0)))
    t = time.strptime(datestring,"%b %d, %Y")
    return date(t.tm_year, t.tm_mon, t.tm_mday)
